/*
 * Copyright (C), 2014-2014, 联创车盟汽车服务有限公司
 * FileName: UserService.java
 * Author:   孙昊(sunhao@lianchuang.com)
 * Date:     2014年12月10日 上午9:42:36
 * Description:
 * History:
 * <author>   <time>    <version>    <desc>
 * 修改人姓名                 修改时间                 版本号                               描述
 */
package com.linkage.admin.service;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.linkage.admin.repository.AdminRepositoryMapper;
import com.linkage.book.pojo.Book;
import com.linkage.category.pojo.Category;
import com.linkage.category.repository.CategoryRepository;
import com.linkage.user.pojo.User;
import com.linkage.user.service.UserService;

/**
 * <br>
 *
 * @author 孙昊(sunhao@lianchuang.com)
 */
@Service
public class AdminService {
    @Autowired
    private AdminRepositoryMapper adminRepositoryMapper;
    
    @Autowired
    private CategoryRepository categoryRepository; 
    
    @Autowired
    private UserService userService;

    public List<User> queryAll() {
    	List<User> list = this.adminRepositoryMapper.queryAll();
    	System.out.println("11111111111111111");
        return list;
    }

    public void save(User user) {
//    	user.setUserId(-1);
        this.adminRepositoryMapper.save(user);
    }

    public void update(User user) {
        this.adminRepositoryMapper.update(user);
    }

    public void delete(String userId) {
        this.adminRepositoryMapper.delete(userId);
    }
    
    public User findUserByName(String username){
        return this.adminRepositoryMapper.findUserByName(username);
    }

	public User findUserById(int userId) {
		return this.adminRepositoryMapper.findUserById(userId);
	}

	public void addBook(Book book) {
		adminRepositoryMapper.addBook(book);
		
	}

	public void deleteBookById(String bookId) {
		//TODO 加上bookId是否存在的校验。
		adminRepositoryMapper.deleteBookById(bookId);
	}

	public void addCategory(Category category) {

		categoryRepository.addCategory(category);
	}

	public void deleteCategoryById(int categoryId) {
		// TODO Auto-generated method stub
		this.categoryRepository.deleteCategoryById(categoryId);
		
	}

	public void UpdateCategory(Category category) {
		
		this.categoryRepository.UpdateCategory(category);
	}

	public List<Book> queryAllBook() {
		List<Book> bookList = this.adminRepositoryMapper.queryAllBook();
		return bookList;
	}

	public void updateBook(Book book) {

		this.adminRepositoryMapper.updateBook(book);
	}


	public boolean adminLogin(HttpServletRequest request, String userName,
			String password) {
		 if (StringUtils.isEmpty(userName)) {
	            return false;
	        }

	        User user = this.userService.loadByLoginName(userName);

	        if (user != null && password.equals(user.getPassword())) {
	            storeInSession(request.getSession(), "LOGIN_USER_KEY_IN_SESSION", user);
	            return true;
	        }

	        return false;
	}
	
	   private void storeInSession(HttpSession session, String key, Object object) {
	        session.setAttribute(key, object);
	    }

}
